/*
 * Copyright 2022-2023 the original author(https://github.com/wj596)
 * 
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */
package org.jsets.fastboot.flowable.controller;

import org.jsets.fastboot.model.DataResp;
import org.jsets.fastboot.model.ListResp;
import org.jsets.fastboot.flowable.model.entity.FlowTask;
import org.jsets.fastboot.flowable.model.dto.CompleteTaskParam;
import org.jsets.fastboot.flowable.model.dto.CompleteTaskResult;
import org.jsets.fastboot.flowable.model.dto.FlowTaskQuery;
import org.jsets.fastboot.flowable.service.IFlowTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author wangjie (https://github.com/wj596)
 * @date 2021.06.25 11:23
 */
@Api(tags = "模型管理")
@RestController
@RequestMapping("/flow/tasks")
public class FlowTaskController {

	@Autowired
	private IFlowTaskService flowTaskService;

	@ApiOperation("查询用户的待办任务")
	@GetMapping(value = "/by_user/{handle_user}", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="page_no", value="当前页码", required=false, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="page_size", value="每页条数", required=false, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="handle_user", value="办理人", required=false, dataType = "String", dataTypeClass = String.class, paramType="path")
	})
	public ListResp<FlowTask> selectByUser(
			@RequestParam(name = "page_no", required = false) Integer pageNo,
			@RequestParam(name = "page_size", required = false) Integer pageSize,
			@PathVariable(name = "handle_user", required = true) String handleUser) {

		FlowTaskQuery criteria = new FlowTaskQuery()
				.setCurrent(pageNo)
				.setSize(pageSize)
				.setUsername(handleUser);
		
		ListResp<FlowTask> respond = ListResp.ok();
		if(criteria.isPageNecessary()) {
			Page<FlowTask> result = this.flowTaskService.selectPage(criteria);
			respond.setData(result.getRecords());
			respond.setTotalCount(result.getTotal());
			return respond;
		}
		respond.setData(this.flowTaskService.selectList(criteria));
		return respond;
	}
	
	@ApiOperation("查询待办任务")
	@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="page_no", value="当前页码", required=false, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="page_size", value="每页条数", required=false, dataType = "Integer", dataTypeClass = Integer.class, paramType="query")
		,@ApiImplicitParam(name="business_key ", value="业务主键", required=false, dataType = "String", dataTypeClass = String.class, paramType="query")
		,@ApiImplicitParam(name="start_user", value="流程发起人", required=false, dataType = "String", dataTypeClass = String.class, paramType="query")
		,@ApiImplicitParam(name="handle_user", value="任务办理人", required=false, dataType = "String", dataTypeClass = String.class, paramType="query")
		,@ApiImplicitParam(name="process_key", value="流程定义KEY", required=false, dataType = "String", dataTypeClass = String.class, paramType="query")
		,@ApiImplicitParam(name="process_name", value="流程定义名称", required=false, dataType = "String", dataTypeClass = String.class, paramType="query")
		,@ApiImplicitParam(name="instance_id", value="流程实例主键", required=false, dataType = "String", dataTypeClass = String.class, paramType="query")
		,@ApiImplicitParam(name="instance_create_time_start", value="流程发起时段(开始)", required=false, dataType = "String", dataTypeClass = String.class, paramType="query")
		,@ApiImplicitParam(name="instance_create_time_end", value="流程发起时段(结束)", required=false, dataType = "String", dataTypeClass = String.class, paramType="query") 
	})
	public ListResp<FlowTask> list(
			@RequestParam(name = "page_no", required = false) Integer pageNo,
			@RequestParam(name = "page_size", required = false) Integer pageSize,
			@RequestParam(name = "business_key", required = false) String businessKey,
			@RequestParam(name = "start_user", required = false) String startUser,
			@RequestParam(name = "handle_user", required = false) String handleUser,
			@RequestParam(name = "process_key", required = false) String processKey,
			@RequestParam(name = "process_name", required = false) String processName,
			@RequestParam(name = "instance_id", required = false) String instanceId,
			@RequestParam(name = "instance_create_time_start", required = false) String instanceCreateTimeStart,
			@RequestParam(name = "instance_create_time_end", required = false) String instanceCreateTimeEnd
			) {

		FlowTaskQuery criteria = new FlowTaskQuery()
				.setCurrent(pageNo)
				.setSize(pageSize)
				.setProcessInstanceBusinessKey(businessKey)
				.setProcessInstanceStartUser(startUser)
				.setUsername(handleUser)
				.setProcessDefinitionKey(processKey)
				.setProcessDefinitionName(processName)
				.setProcessInstanceId(instanceId)
				.setProcessInstanceCreateTimeStart(instanceCreateTimeStart)
				.setProcessInstanceCreateTimeEnd(instanceCreateTimeEnd);
		
		ListResp<FlowTask> respond = ListResp.ok();
		if(criteria.isPageNecessary()) {
			Page<FlowTask> result = this.flowTaskService.selectPage(criteria);
			respond.setData(result.getRecords());
			respond.setTotalCount(result.getTotal());
			return respond;
		}
		respond.setData(this.flowTaskService.selectList(criteria));
		return respond;
	}

    @PostMapping(value = "/complete", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "办理任务")
    public DataResp<CompleteTaskResult> complete(@RequestBody CompleteTaskParam param) {
		DataResp<CompleteTaskResult> respond = DataResp.ok();
		respond.setData(this.flowTaskService.complete(param));
		return respond;
    }
    
}